package com.goodving.guli.application;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.accessibility.AccessibilityEventCompat;
import android.text.TextUtils;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.Configuration;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.lua.C1226h;
import com.goodving.guli.bt.lua.DaoManager;
import com.goodving.guli.bt.model.DataManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.XMBLEManager;
import com.goodving.guli.bt.weight.C0892e;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.utility.C1481u;
import com.goodving.guli.view.C0804t;
import com.goodving.guli.view.C4220a;

import cn.com.smartdevices.bracelet.chart.typeface.MIUITextStyleExtractor;
import cn.com.smartdevices.bracelet.chart.typeface.TypefaceManager;
import cn.com.smartdevices.bracelet.config.Config;
import name.bagi.levente.pedometer.StepService;

/**
 * Created by Administrator on 2015-11-17.
 */
public class GuliApplication extends Application {
    public static final String TAG = GuliApplication.class.getSimpleName();
    private RequestQueue mRequestQueue;
    private static GuliApplication mInstance;
    private static StepService mStepService =  new StepService();

    public void onCreate() {
        super.onCreate();
        mInstance = this;
        C1481u.setContext(getApplicationContext());
        Keeper.init(this);
        Keeper.setEnterAppTrace(2);
        try {
            Config.m8860a(this, true);
        } catch (Throwable e) {
            //TBD
        }
        ActiveAndroid.initialize(this);

        // init the datamanager
        DataManager.m12181a((Context) this);
        DaoManager.m11664a(this);

        GuliLog.tracePrintWarn(TAG, "Init DB!!");
        Configuration.Builder aaConfig = new Configuration.Builder(this);
       //C2112a.m16243a(new C2143e(this).m16447a((int) C0422a.f1478n).m16453b(C2130c.f11946b).m16446a(), Config.f4188x);
        C1226h.m11373a(this);
        ActiveAndroid.initialize(aaConfig.setCacheSize((int) AccessibilityEventCompat.TYPE_TOUCH_INTERACTION_START).setSqlParser(Configuration.SQL_PARSER_DELIMITED).create(),
                Config.f4188x);

//        startStepService();
        // start the ble service
        XMBLEManager.startBleServiceForDevice(C1481u.getContext(), Keeper.readBraceletBtInfo().f19636b, Utils.m8093p());
        Utils.handleUUID(this);
        Utils.m8094p(this);
        TypefaceManager.m8723a(MIUITextStyleExtractor.m8720a());
        Config h = Config.m8876h();
        // C0668a.m8135a is for umeng
//        C0668a.m8135a(h.f4201k.f4232a.booleanValue(), Config.f4188x, h.f4201k.f4233b.booleanValue(), (Context) this);
//        C1218a.m11325a((Context) this);

        if (C0804t.m8839d(C1481u.getContext())) {
            C0804t.m8829a((Context) this, (int) C4220a.ax);
        }
        C0892e.m9081a().m9082a(Utils.m8088m(this));

//        // create db table for lualist
//        LuaList luaList = new LuaList();
//        LuaZipFile luaZipFile = new LuaZipFile();
//
//        PersonInfo readPersonInfo = Keeper.readPersonInfo();
//        // TBD hack here
//        readPersonInfo.uid = 100;
//        readPersonInfo.nickname = "storm";
//        Keeper.keepPersonInfo(readPersonInfo);

    }

    public void startStepService() {
        mStepService.startService(new Intent(this, StepService.class));
    }


    public static synchronized GuliApplication getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
    
}
